package com.guoran100.adphoto.display.activity;

import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.guoran100.adphoto.R;
import com.guoran100.adphoto.display.view.IPlayView;
import com.guoran100.adphoto.presenter.contract.IPlayPresenter;
import com.guoran100.adphoto.presenter.implement.PlayPresenter;
import com.recker.flybanner.FlyBanner;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class PlayActivity extends AppCompatActivity implements IPlayView{

    private IPlayPresenter iPlayPresenter;

    private FlyBanner mBannerNet;//加载网络图片
    List<String> imgesUrl = new ArrayList<>();

    private String[] mImagesUrl = {
            "http://android.tgbus.com/bizhi/UploadFiles_7790/201112/20111202180757257.jpg",
            "http://attach.bbs.miui.com/forum/201502/12/213031z8r4kfizz9ww3447.jpg",
            "http://img1.mydrivers.com/img/20150331/6052fe56b3cd4ec286bcc300b2c494ca.png",
            "http://attach.bbs.miui.com/forum/201412/13/134020rhrrovvvtustvqpu.png"
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        iPlayPresenter=new PlayPresenter(this,this);

        mBannerNet = (FlyBanner) findViewById(R.id.banner_1);
        mBannerNet.setPointsIsVisible(false);
        timer.schedule(task, 1000, 60000); // 1s后执行task,经过1s再次执行



    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }


    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                imgesUrl.clear();
                iPlayPresenter.getPhotosList(); //更新一次
            }
            super.handleMessage(msg);
        };
    };
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {

        @Override
        public void run() {
            // 需要做的事:发送消息
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
        }
    };


    @Override
    public void getPhotosListOk(@NonNull List<JSONObject> result) {
        //Toast.makeText(this,"联机",Toast.LENGTH_SHORT).show();
        for (int i = 0; i < result.size(); i++) {
            imgesUrl.add(result.get(i).getString("fileurl"));
        }
        mBannerNet.setImagesUrl(imgesUrl);
    }
}
